
import java.util.Date;
import java.util.Observable;
import java.util.Timer;
import java.util.TimerTask;
import java.util.Vector;

/**
 *
 * @author David Rodriguez A.
 */

public class Valve extends Observable{
    private int preasure, level, id, sec;
    private String state;
    private boolean b;
    private Timer timer;
    private Vector <Valve> valves;
    private AppValveViewer AppViewer; //referencia a la interfaz
    private int x=0; //variable que indica si empeso denuevo
    private boolean less = false;
    /**
     * Constructor: inicia variables de valve
     *
     * @param sec parametro para setear los segundos del timer
     * @param id  parametro id del valve
     */
    
    public Valve(int sec, int id){
        this.preasure = 0;
        this.level = 50;
        this.sec = sec;
        this.id = id;
        this.valves = new Vector<Valve>(); //inicia vector de valves
        this.timer = new Timer();
        this.timer.scheduleAtFixedRate(timerTask, 0, 100*sec);
    }
    
    public void setPreasure(int p){
        preasure = p;
    }

    public void setState(String state){
        this.state = state;
    }


    public String getState(){
        return this.state;
    }

    public void setLevel(int l){
        level = l;
    }

    public void stopTimer(){
        timer.cancel();
        
    }

    public void setBoolean(){
        this.b = true;
    }

    /**
     * attachValve pega el objeto valve pasado por argumento al valve actual
     *
     * @param v Objeto a pegar
     */
    public void attachValve(Valve v){
        this.valves.add(v);
        System.out.println("attachValve, vector:  "+this.valves);
    }

    public void detachValve(Valve v){
        this.valves.remove(v);
        System.out.println("detach, valves: "+this.valves);
    }

    public int getLevel(){
        return this.level;
    }
    
    public int getId(){
        return this.id;
    }
    
    public void turnON(){
        this.b = true;
        this.setState("ON");
    }

    public void turnOFF(){
        this.b=false;
        this.setState("OFF");
    }

   /**
    * operation: Funcion que efectua la operación del valve.
    * @param b   Indica si la operacion se efectua o no.
    */
    public void operation(boolean b){
        if(b){
            //level ++;
            //calcula nuevo valor de level
            if(less!=true){
                if(this.x<50){
                	if (AppViewer.getTempMode())
                		x += new Double(Math.random() *50).intValue();
                	else
                		x += new Double(Math.random() *10).intValue();
                    level = x;
                }else{
                	if (AppViewer.getTempMode())
                		level = 100 - new Double(Math.random() *10).intValue();
                	else
                		level = 55 - new Double(Math.random() *10).intValue();
                 }
            }else{ //se pauso la operacion
                    if(level!=0) level--;
            }
            
            this.updateProgress(); //actualiza el progressbar
            //System.out.println("Valve_id: "+id+" level: "+level);
        }
    }
    

    public void setListener(AppValveViewer v){
        this.AppViewer=v;
    }

    public void updateProgress(){
        this.AppViewer.setProgressValue(level);
        
    }

    public void pauseOn(){
        this.less=true;
    }

    public void pauseOff(){
        this.less = false;
    }

    public boolean getLess(){
        return this.less;
    }
    


    /**
     * TimerTask
     * Método run sera llamado por Timer cada segundo.
     * Se encarga de avisar a los observadores de este modelo.
     */

    TimerTask timerTask = new TimerTask()
     {
        public void run()
        {
            setChanged();
            notifyObservers(new Date());
            operation(b); /*ejecuta operacion del valve*/
        }
    };
}
